<script>
  jQuery(document).ready(function(){
    create_header('#team_stats', '500px');
    create_header('#compare_teams');
    create_header('#team_players_stats');
    create_header('#team_history');
    
    show_table_partial("#team_players_stats");
    jQuery("#team_players_stats a").click();
    
    show_table_partial("#team_history_stats");
    jQuery("#team_history_stats a").click();
    
    jQuery(".tablesorter").tablesorter({sortList: [[0,0],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1],[8,1]]}); 
  });
</script>

<form action="index.php" method="post" name="adminForm">
  <div id="tabs" style="padding-top:10px;">
    <ul>
    <? foreach($this->divisions as $division){ ?>
      <li><a href="<?= JURI::base(true) ?>/component/pbny/teams#conference-<?= $division->id?>"><?= $division->name ?></a></li>
    <? } ?>
    </ul>
  </div>
  <div style="margin:5px 10px 10px;padding:0px 10px 5px;">
    <h2 style="margin:0px;padding:0px; width:100%;"><?= $this->team->name ?><span style="float:right; margin-right:60px;font-size:1.2em"><?= $this->team->abbr ?></span></h2>
  </div>  
  
  <? $logo = null;
      if(isset($this->team->logo))
      $logo = $this->team->logo;
  ?>
  <div style="float:left; border:solid 1px #9D9D9D; padding:2px; width:200px; margin-left:10px;">
    <img src="<?= PBNYHelper::getFlickrImageUrl($logo->flickr_id, 'small') ?>" width="200px" height="240px" alt="no image"/> <!-- profile pic, i need to fix this -->
  </div>
  <? $pic = null;
     if(isset($this->team->pic))
       $pic = $this->team->pic;
  ?>
  <div style="float:right; border:solid 1px #9D9D9D; padding:2px; width:200px; margin-right:10px;">
    <img src="<?= PBNYHelper::getFlickrImageUrl($pic->flickr_id, 'small') ?>" width="200px" height="240px" alt="no image"/> <!-- random pic -->
  </div>    
  
  <? $cols = array();
     $cols['PPG']  = 'avg_points';
     $cols['FTPG'] = 'avg_free_throws';
     $cols['FPG']  = 'avg_field_goals';
     $cols['3PPG'] = 'avg_three_points';
     $cols['ORPG'] = 'avg_off_rebounds';
     $cols['DRPG'] = 'avg_def_rebounds';
     $cols['APG']  = 'avg_assists';
     $cols['SPG']  = 'avg_steals';
     $cols['BPG']  = 'avg_blocks';
  ?>
  
  <div id="team_stats">
    <? if(isset($this->team->team_game_stats)): ?>
      <? $stat = $this->team->team_game_stats; ?>
        <table style="width:450px; margin:auto;">
          <thead>
            <tr>
              <td></td>
              <td style="background-color:#111;">HME</td>
              <td style="background-color:#111;">OPP</td>
              <td style="background-color:#111;">DIFF</td>
            </tr>
          </thead>
          <tbody>
            
            <? foreach($cols as $key=>$value){ ?>
              <tr>
                <td style="background-color:#111;"><?= $key ?></td>
                <td style="background-color:#333;"><?= $stat['HME'][$value] ?></td>
                <td style="background-color:#333;"><?= $stat['OPP'][$value] ?></td>
                <td style="background-color:#333;"><?= $stat['DIFF'][$value] ?></td>
              </tr>
            <? } ?>
            
          </tbody>
        </table>    
    <? else: ?>
      <div style="line-height:300px;">
        <h2>No team stats found.</h2>
      </div>
    <? endif; ?>
  </div>
  
  <div id="team_history" style="padding-top:10px;">
    <h3>Team History:</h3>
    <? if(isset($this->team->history)): 
        $history = $this->team->history;
    ?>
    <div id="team_history_stats">
    <div style="padding:5px;font-weight:bold;"><?= $this->season->name ?></div>
    <table style="width:100%; margin:20px 0px;" class="tablesorter">
      <thead>
        <tr>
          <th class="cell_key">OPPONENT TEAM</th>
          <th class="cell_key">SCHEDULE</th>
          <th class="cell_key">POINTS</th>
        
          <th class="cell_key">FG</th>
          <th class="cell_key">FGA</th>
          
          <th class="cell_key">TP</th>
          <th class="cell_key">TPA</th>
          
          <th class="cell_key">FT</th>
          <th class="cell_key">FTA</th>
          
          <th class="cell_key">OFF REB</th>
          <th class="cell_key">DEF REB</th>
          
          <th class="cell_key">ASSISTS</th>
          <th class="cell_key">STEALS</th>
          <th class="cell_key">BLOCKS</th>
          
        </tr>
      </thead>
      <tbody>
      <?    foreach($history as $schedule_id => $stat): ?>
          <tr>
            <td class="cell_value" align="left" style="padding-left:20px"><?= $this->teams[$stat->opponent_team_id]->name ?></td>
            <td class="cell_value"><?= $stat->schedule ?></td>
            
            <td class="cell_value"><?= $stat->points ?></td>
            <td class="cell_value"><?= $stat->field_goals ?></td>
            <td class="cell_value"><?= $stat->field_goal_attempts ?></td>
            
            <td class="cell_value"><?= $stat->three_points ?></td>
            <td class="cell_value"><?= $stat->three_point_attempts ?></td>
            <td class="cell_value"><?= $stat->free_throws ?></td>
            <td class="cell_value"><?= $stat->free_throw_attempts ?></td>
            
            <td class="cell_value"><?= $stat->off_rebounds ?></td>
            <td class="cell_value"><?= $stat->def_rebounds ?></td>
            
            <td class="cell_value"><?= $stat->assists ?></td>
            <td class="cell_value"><?= $stat->steals ?></td>
            <td class="cell_value"><?= $stat->blocks ?></td>
            
          </tr>
    <?    endforeach; ?>
        </tbody>
    </table>
    <? if(count($history) > 5): ?>
      <a href="" style="margin-left:900px;margin-bottom:10px">Show All</a>
    <? endif; ?>
    <? else: ?>
      <div style="line-height:80px;">
        <h2>No team history found.</h2>
      </div>
    <? endif; ?>
  </div>
  </div>
  
  <div id="team_players_stats" style="padding-top:10px;">
    <h3>Players Statistics:</h3>
    <table style="width:100%; margin:20px 0px;" class="tablesorter">
      <thead>
        <tr>
          <th class="cell_key">Player</th>
          <th class="cell_key">GP</th>
          <th class="cell_key">PTS</th>
          <th class="cell_key">PPG</th>
          <th class="cell_key">OFF REB</th>
          <th class="cell_key">DEF REB</th>
          <th class="cell_key">ASSTS</th>
          <th class="cell_key">STLS</th>
          <th class="cell_key">BLKS</th>
        </tr>
      </thead>
      <tbody>
        <? foreach($this->players as $player): 
          $points       = 0;
          $ppg          = 0;
          $off_rebounds = 0;
          $def_rebounds = 0;
          $assists      = 0;
          $steals       = 0;
          $blocks       = 0;
          
          if(isset($player->points)){
            $ppg = number_format($player->points/$player->gp, 2);
            $points = $player->points;
          }
          if(isset($player->def_rebounds))
            $def_rebounds = number_format($player->def_rebounds/$player->gp, 2);
          if(isset($player->off_rebounds))
            $off_rebounds = number_format($player->off_rebounds/$player->gp, 2);
          if(isset($player->assists))
            $assists = number_format($player->assists/$player->gp, 2);
          if(isset($player->steals))
            $steals = number_format($player->steals/$player->gp, 2);
          if(isset($player->blocks))
            $blocks = number_format($player->blocks/$player->gp, 2);
        ?>
          <tr>
            <td class="cell_value"><?= $player->fullname ?></td>
            <td class="cell_value"><?= $player->gp ?></td>
            <td class="cell_value"><?= $points ?></td>
            <td class="cell_value"><?= $ppg ?></td>
            <td class="cell_value"><?= $off_rebounds ?></td>
            <td class="cell_value"><?= $def_rebounds ?></td>          
            <td class="cell_value"><?= $assists ?></td>
            <td class="cell_value"><?= $steals ?></td>
            <td class="cell_value"><?= $blocks ?></td>
          </tr>
        <? endforeach ?>
      </tbody>
    </table>
    <? if(count($this->players) > 5): ?>
      <a href="" style="margin-left:900px;margin-bottom:10px">Show All</a>
    <? endif; ?>
  </div>

  <div id="compare_teams" style="padding-top:10px;">
    <h3>Compare with:</h3>
    <p>
      Select Team:<?= $result  = JHTML::_('select.genericlist', $this->teams, 'team_id', null, 'id', 'name', null);?>
      <? $link = JURI::base(true)."/component/pbny/teams/compare_teams/".$this->team->id; ?>
      <input type="button" onclick="javascript:redirect('<?= $link ?>', jQuery('#team_id').val());" value="Compare">
    </p>
  </div>
</form>